from flask_restful import Resource

from restful.api__.public__.info.composite.post.list._PostsRank import _PostsRank
from restful.api__.session import login_required_current_user_generate
from restful.model import db
from restful.model.query import QueryMultipleInstances
from restful.model.tables import Blog, Check
from restful.response import ResponseSuccess


class PostsRank10TopRecent(Resource):
    @login_required_current_user_generate
    def get(self, current_user):
        # 获取所有已审核通过的帖子
        checks = QueryMultipleInstances(Check, {'checked': True, 'passed': True})
        valid_pids = [check.pid for check in checks]

        posts = (
            db.session.query(Blog)
            .filter(Blog.id.in_(valid_pids))
            .order_by(Blog.time.desc())
            .limit(10)
            .all()
        )

        return ResponseSuccess(data=_PostsRank(posts))


